package com.wzw.shoppingcart.resolver;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wzw.shoppingcart.entity.Product;
import com.wzw.shoppingcart.mapstruct.PageMapper;
import com.wzw.shoppingcart.service.ProductService;
import com.wzw.shoppingcart.vo.PageVo;
import graphql.kickstart.tools.GraphQLQueryResolver;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.List;


/**
 *@author wzw
 * 2023/4/7
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class ProductQueryResolver implements GraphQLQueryResolver {

    private final ProductService productService;

    public PageVo productList(Integer pageNum, Integer pageSize, String keyword) {
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        if (StringUtils.hasText(keyword)) {
            queryWrapper.like("product_name", keyword);
        }
        Page<Product> page = productService.page(new Page<>(pageNum, pageSize), queryWrapper);
        PageVo pageVo = PageMapper.INSTANCE.pageToPageVo(page);
        List<Product> productList = page.getRecords();
        productService.setDiscountType(productList);
        pageVo.setProductList(productList);
        return pageVo;
    }


}
